package com.erp.erp_ui.Blueprint;

import com.erp.erp_entitys.Blueprint.BlueprintContentFile;
import com.erp.erp_entitys.Blueprint.BlueprintEntity;
import com.erp.erp_entitys.Blueprint.BlueprintFileEntity;
import com.erp.erp_entitys.Blueprint.BlueprintProductEntity;
import com.erp.erp_entitys.Blueprint.req.AddContentFileReq;
import com.erp.erp_entitys.Blueprint.res.AddBlueprintContentRes;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Blueprint.IBlueprintContentFileService;
import com.erp.erp_servers.Blueprint.IBlueprintFileService;
import com.erp.erp_servers.Blueprint.IBlueprintProductService;
import com.erp.erp_servers.Blueprint.IBlueprintService;
import com.erp.erp_servers.Ftp.IFtpService;
import com.erp.erp_servers.ProductMainInfo.IProductMainInfoService;
import com.erp.erp_servers.User.IUserService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName BlueprintControl
 * @Description 图纸控制类
 * @Author xumingbo
 * @Date 2020-09-07
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "blueprint")
@Api( tags = "图纸")
public class BlueprintControl extends Base {
    @Autowired
    IBlueprintService blueprintService;
    @Autowired
    IBlueprintProductService blueprintProductService;
    @Autowired
    IBlueprintFileService blueprintFileService;
    @Autowired
    IFtpService ftpService;
    @Autowired
    IUserService userService;
    @Autowired
    IProductMainInfoService productMainInfoService;
    @Autowired
    IBlueprintContentFileService blueprintContentFileService;

    SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");







    /**
     * 新增主信息，明细信息
     * @param blueprint
     * @return 操作记录数
     */
    @RequestMapping("add")
    @ResponseBody
    public R add(@Validated @RequestBody BlueprintEntity blueprint) {
        try {
            if(StringUtils.isEmpty(blueprint.getNumber())){
                return  R.buildError(ResultStatusEnum.NOT_EMPTY);
            }
            boolean isExist = blueprintService.isExist(blueprint.getNumber());
            if(isExist){
                return  R.buildError(ResultStatusEnum.BILL_NO_REPEAT);
            }
            if(blueprint.getCreateTime()==null){
                blueprint.setCreateTime(new Date());
            }
            blueprint.setBoxStatus("1");//草稿
            AddBlueprintContentRes res=blueprintService.add(blueprint);
            return R.build(res);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }














    /**
     * 新增图纸
     * @param blueprint
     * @return 操作记录数
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增图纸", notes ="新增图纸", httpMethod = "POST")

    public R<Integer> insert(@Validated @RequestBody BlueprintEntity blueprint) {
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                if(StringUtils.isEmpty(blueprint.getNumber())){
                    return  R.buildError(ResultStatusEnum.NOT_EMPTY);
                }
                boolean isExist = blueprintService.isExist(blueprint.getNumber());
                if(isExist){
                    return  R.buildError(ResultStatusEnum.BILL_NO_REPEAT);
                }
                UserEntity user = (UserEntity )cacheUser;
                if (blueprint.getOperator()==null || blueprint.getOperator().equals("")){
                    blueprint.setOperator(user.getCode());
                }
                if(blueprint.getCreateTime()==null){
                    blueprint.setCreateTime(new Date());
                }
                blueprint.setBoxStatus("1");//草稿
                int num = blueprintService.insertInfo(blueprint);
//                int num = blueprintService.insertSelective(blueprint);
//                if(num!=0){
//                    Integer bId = blueprint.getId();
//                    List<BlueprintProductEntity> productList = blueprint.getProductList();
//                    int proNum = 0;
//                    if(!CollectionUtils.isEmpty(productList)){
//                        for (BlueprintProductEntity blueprintProduct : productList){
//                            if (blueprintProduct!=null){
//                                blueprintProduct.setBlueprintId(bId);
//                                proNum += blueprintProductService.insertSelective(blueprintProduct);
//                            }
//                        }
//                    }
//                    List<BlueprintFileEntity> fileList = blueprint.getFileList();
//                    int fileNum = 0;
//                    if(!CollectionUtils.isEmpty(fileList)){
//                        for(BlueprintFileEntity blueprintFile : fileList){
//                            if (blueprintFile!=null){
//                                blueprintFile.setBlueprintId(bId);
//                                fileNum += blueprintFileService.insertSelective(blueprintFile);
//                            }
//                        }
//                    }
//                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
           return  R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 按主键id删除图纸
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除图纸", notes = "按id删除图纸", httpMethod = "POST")
    public R<Integer> delete(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                BlueprintEntity blueprint = blueprintService.selectInfoById(id);
                int num = 0;
                if(blueprint!=null){
                    if(!CollectionUtils.isEmpty(blueprint.getProductList())){
                        for (BlueprintProductEntity pro : blueprint.getProductList()){
                            blueprintProductService.deleteByPrimaryKey(pro.getId());
                        }
                    }
                    if(!CollectionUtils.isEmpty(blueprint.getFileList())){
                        for (BlueprintFileEntity file : blueprint.getFileList()){
                            blueprintFileService.deleteByPrimaryKey(file.getId());
                        }
                    }
                    num = blueprintService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据id查询图纸-产品-附件
     * @param id
     * @return 图纸实体类
     */
    @RequestMapping(value = "selectByKey")
    @ResponseBody
    public  R selectByKey(@RequestBody Integer id) {
        try {
            BlueprintEntity BlueprintEntity = blueprintService.selectInfoById(id);
            return R.build(BlueprintEntity);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 根据id更新图纸
     * 部分字段
     * @param blueprint
     * @return 操作记录数
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按id更新图纸", notes ="按id更新图纸", httpMethod = "POST")

    public R<Integer> update(@RequestBody BlueprintEntity blueprint) {
        try {
            UserEntity user = (UserEntity )this.getBean("user");
            if (null!=user){
                int num = blueprintService.updateById(blueprint);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
           return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新图纸审核状态
     * 部分字段
     * @param map
     * @return 操作记录数
     */
    @RequestMapping(value = "updateBoxStatusBykey")
    @ResponseBody
    @ApiOperation(value = "按id更新图纸审核状态", notes = "按id更新图纸审核状态", httpMethod = "POST")
    public R<Integer> updateBoxStatusBykey(@RequestBody Map<String, Object> map){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                if(StringUtils.isEmpty(map.get("id").toString()) || StringUtils.isEmpty(map.get("boxStatus").toString())){
                    return  R.buildError(ResultStatusEnum.PARAMETER_ERROR);
                }
                int num = blueprintService.updateBoxStatusBykey(map);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据给定的条件查询图纸
     * @param boxStatus 单据类型
     * @param numberLike 单据信息
     * @param operator 所属操作员
     * @param startCreateTime 创建开始时间
     * @param endCreateTime 创建结束时间
     * pageNum 当前页数
     * pageSize 每页条数
     * @return 图纸实体类列表
     */
    @RequestMapping(value = "selectByPage",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据条件查询图纸", notes ="根据条件查询图纸", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="boxStatus",value = "单据类型"),
            @ApiImplicitParam(name="numberLike",value = "单据信息"),
            @ApiImplicitParam(name="operator",value = "所属操作员"),
            @ApiImplicitParam(name="startCreateTime",value = "创建开始时间"),
            @ApiImplicitParam(name="endCreateTime",value = "创建结束时间"),
            @ApiImplicitParam(name="productNo",value = "产品编码"),
            @ApiImplicitParam(name="pageNum",value = "当前页"),
            @ApiImplicitParam(name="pageSize",value = "每页条数")
    })
    public R/*<List<BlueprintEntity>>*/ selectByPage(
            String boxStatus, String numberLike, String operator,
            String startCreateTime, String endCreateTime, String productNo){
        try {
            Map<String,String> map = new HashMap<String,String>();
            if(StringUtils.isNotEmpty(boxStatus)){
                map.put("boxStatus",boxStatus);
            }
            if(StringUtils.isNotEmpty(numberLike)){
                map.put("numberLike",numberLike);
            }
            if(StringUtils.isNotEmpty(operator)){
                map.put("operator",operator);
            }
            if (!StringUtils.isEmpty(startCreateTime)){
                Date startDate = DateUtils.getStartOfDay(DateUtils.convertStringToDate(startCreateTime,"yyyy-MM-dd"));
                map.put("startCreateTime",DateUtils.format(startDate,"yyyy-MM-dd HH:mm:ss"));
                Date endDate = null;
                if (StringUtils.isEmpty(endCreateTime)){
                    endDate = new Date(System.currentTimeMillis());
                }
                endDate = DateUtils.getEndOfDay(DateUtils.convertStringToDate(endCreateTime,"yyyy-MM-dd"));
                map.put("endCreateTime",DateUtils.format(endDate,"yyyy-MM-dd HH:mm:ss"));
            }
            if(StringUtils.isNotEmpty(productNo)){
                map.put("productNo",productNo);
            }
            PageInfo<BlueprintEntity> pageInfo = blueprintService.selectByPage(map);
            return R.build(200,"", pageInfo.getTotal(), pageInfo.getList());
//            List<BlueprintEntity> list = blueprintService.selectByPage(map);
//            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增图纸产品
     * @param blueprintProduct
     * @return 操作记录数
     */
    @RequestMapping(value = "insertProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增图纸产品", notes ="新增图纸产品", httpMethod = "POST")

    public R<Integer> insertProduct(@RequestBody BlueprintProductEntity blueprintProduct) {
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = blueprintProductService.insertSelective(blueprintProduct);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 根据主键id查询图纸产品 
     * @param id
     * @return 图纸产品实体类
     */
    @RequestMapping(value = "selectProductByKey/{id}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据id查询图纸产品", notes ="根据id查询图纸产品", httpMethod = "POST")

    public  R<BlueprintProductEntity> selectProductByKey(@PathVariable("id")Integer id) {
        try {
            BlueprintProductEntity blueprintProduct = blueprintProductService.selectByPrimaryKey(id);
            return R.build(blueprintProduct);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }
    /**
     * 根据id更新图纸产品
     * @param blueprintProduct
     * @return 操作记录数
     */
    @RequestMapping(value = "updateProduct",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据id更新图纸产品", notes ="根据id更新图纸产品", httpMethod = "POST")

    public R<Integer> updateProduct(@RequestBody BlueprintProductEntity blueprintProduct) {
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = blueprintProductService.updateByPrimaryKeySelective(blueprintProduct);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除图纸-产品
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteProduct/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除图纸产品", notes = "按id删除图纸产品", httpMethod = "POST")
    public R<Integer> deleteProduct(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                BlueprintProductEntity blueprintProduct = blueprintProductService.selectByPrimaryKey(id);
                int num = 0;
                if(blueprintProduct!=null){
                    num = blueprintProductService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增图纸附件
     * @param blueprintFile
     * @return 操作记录数
     */
    @RequestMapping(value = "insertFile" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增图纸附件", notes = "新增图纸附件", httpMethod = "POST")
    public R<Integer> insertFile(@RequestBody BlueprintFileEntity blueprintFile){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = blueprintFileService.insertSelective(blueprintFile);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (DataIntegrityViolationException e1){
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.DATA_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询图纸附件
     * @param id
     * @return 图纸附件实体
     */
    @RequestMapping(value = "selectFileByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询图纸附件",notes = "按id查询图纸附件",httpMethod = "POST")
    public R<BlueprintFileEntity> selectFileByKey(@PathVariable("id") Integer id){
        try {
            if (id==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            BlueprintFileEntity blueprintFile = blueprintFileService.selectByPrimaryKey(id);
            if (blueprintFile==null){
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(blueprintFile);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新图纸-附件
     * 部分字段
     * @param blueprintFile
     * @return 操作记录数
     */
    @RequestMapping(value = "updateFile")
    @ResponseBody
    @ApiOperation(value = "按id更新图纸", notes = "按id更新图纸产品记录", httpMethod = "POST")
    public R<Integer> updateFile(@RequestBody BlueprintFileEntity blueprintFile){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = blueprintFileService.updateByPrimaryKeySelective(blueprintFile);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除图纸-附件
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteFile/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除图纸附件", notes = "按id删除图纸附件", httpMethod = "POST")
    public R<Integer> deleteFile(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                BlueprintFileEntity blueprintFile = blueprintFileService.selectByPrimaryKey(id);
                int num = 0;
                if(blueprintFile!=null){
                    num = blueprintFileService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按条件查询图纸
     * @param number 产品编号
     * @param specificationNumber 规格编号
     * @param unit 单位
     * @param colour 颜色
     * @param customerArticleNo 客户货号
     * @return 图纸产品实体类列表
     */
    @RequestMapping(value = "selectProductByPage",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按条件查询图纸", notes ="按条件查询图纸", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="number",value = "产品编号"),
            @ApiImplicitParam(name="specificationNumber",value = "规格编号"),
            @ApiImplicitParam(name="unit",value = "单位"),
            @ApiImplicitParam(name="colour",value = "颜色"),
            @ApiImplicitParam(name="customerArticleNo",value = "客户货号"),
            @ApiImplicitParam(name="pageNum",value = "当前页"),
            @ApiImplicitParam(name="pageSize",value = "页内条数")
    })
    public R<List<BlueprintProductEntity>> selectProductByPage(
            String number, String specificationNumber,
            String unit, String colour, String customerArticleNo){
        try {
            Map<String,String> map = new HashMap<String,String>();
            if(number!=null && !number.equals("")){
                map.put("number",number);
            }
            if(specificationNumber!=null && !specificationNumber.equals("")){
                map.put("specificationNumber",specificationNumber);
            }
            if(unit!=null && !unit.equals("")){
                map.put("unit",unit);
            }
            if(colour!=null && !colour.equals("")){
                map.put("colour",colour);
            }
            if(customerArticleNo!=null && !customerArticleNo.equals("")){
                map.put("customerArticleNo",customerArticleNo);
            }
            List<BlueprintProductEntity> list = blueprintProductService.selectByPage(map);
            if(list==null || list.size()==0){
                return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 根据图纸主键id查询图纸产品
     * @param bid
     * @return 图纸产品
     */
    @RequestMapping(value = "selectProductByBid/{bid}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据图纸id查询图纸产品", notes ="根据图纸id查询图纸产品", httpMethod = "POST")

    public  R<List<BlueprintProductEntity>> selectProductByBid(@PathVariable("bid") Integer bid) {
        try {
            List<BlueprintProductEntity> list = blueprintProductService.selectProductByBid(bid);
            if(list==null || list.size()==0){
                return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据图纸主键id查询图纸附件
     * @param bid
     * @return 图纸产品
     */
    @RequestMapping(value = "selectFileByBid/{bid}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据图纸id查询图纸附件", notes ="根据图纸id查询图纸附件", httpMethod = "POST")

    public  R<List<BlueprintFileEntity>> selectFileByBid(@PathVariable("bid")Integer bid) {
        try {
            List<BlueprintFileEntity> list = blueprintFileService.selectFileByBid(bid);
            if(list==null || list.size()==0){
                return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 查询全部 图纸和产品 列表
     * @return 全部图纸及产品 列表信息
     */
    @RequestMapping(value = "selectBlueprintList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询图纸列表", notes ="查询图纸列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="boxStatus",value = "单据类型"),
            @ApiImplicitParam(name="numberLike",value = "单据信息"),
            @ApiImplicitParam(name="executor",value = "所属操作员"),
            @ApiImplicitParam(name="startCreateTime",value = "创建开始时间"),
            @ApiImplicitParam(name="endCreateTime",value = "创建结束时间"),
            @ApiImplicitParam(name="pageNum",value = "当前页"),
            @ApiImplicitParam(name="pageSize",value = "页内条数")
    })
    public  R<List<BlueprintEntity>> selectBlueprintList(@RequestBody Map<String,Object> map) {
        try {
            if (map==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            if(map.get("pageNum")!=null){
                int pageNum = Integer.parseInt(map.get("pageNum").toString());
                if (pageNum<1){
                    map.put("pageNum",1);
                }
            }

            if (map.get("endCreateTime")==null || map.get("endCreateTime").equals("")){
                Date date = new Date(System.currentTimeMillis());
                map.put("endCreateTime",dateFormat.format(date));
            }
            List<BlueprintEntity> list = blueprintService.selectBlueprintList(map);
            if(list==null || list.size()==0){
                return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 按id批量删除图纸
     * @param blueprint
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteByIdList")
    @ResponseBody
    @ApiOperation(value = "按id删除图纸", notes = "按id删除图纸", httpMethod = "POST")
    public R<Integer> deleteByIdList(@RequestBody BlueprintEntity blueprint){
        try {
            Object cacheUser = this.getBean("user");
            if (blueprint==null || blueprint.getIdList()==null || blueprint.getIdList().size()==0){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                int num = blueprintService.deleteByIdList(blueprint.getIdList());
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 查询最新单号  累计
     * @return 单号
     */
    @RequestMapping(value = "getLatestOddNumber")
    @ResponseBody
    @ApiOperation(value = "查询最新单号",notes = "查询最新单号",httpMethod = "POST")
    public R<String> selectByKey(){
        try {
            Integer latestOddNumberCount = blueprintService.getCountNum();
            int digit = 3;//位数
            int max = Integer.parseInt("1"+String.format("%1$0"+digit+"d",0));//右补0
            while (latestOddNumberCount>=max){
                digit++;
                max = Integer.parseInt("1"+String.format("%1$0"+digit+"d",0));
            }
            String latestOddNumber = "";
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String nowDateStr = sdf.format(d);
            String suffix = String.format("%0"+digit+"d", (latestOddNumberCount+1));//左补齐0
            latestOddNumber = "TZ"+nowDateStr+suffix;
            return R.build(latestOddNumber);
        } catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }



    /**
     * 根据主id，明细id 获取附件集合
     * @return
     */
    @RequestMapping(value = "selectBlueprintContentList")
    @ResponseBody
    public R selectBlueprintContentListByPage(@RequestParam("blueprintId") Integer blueprintId,
                                              @RequestParam("detailId") Integer detailId){
        try {
            List<BlueprintContentFile> list = blueprintContentFileService.findContentFileByParam(blueprintId,detailId);
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 新增主信息，明细信息
     * @return 操作记录数
     */
    @RequestMapping("contentUploadData")
    @ResponseBody
    public R contentUploadData(@RequestBody AddContentFileReq req) {
        try {
            if (CollectionUtils.isEmpty(req.getFiles())){
                return R.buildCustomize(-200,"附件为空");
            }
            Boolean flag=blueprintContentFileService.insertSelective(req);
            if (flag){
                return R.build(ResultStatusEnum.SUCCESS);
            }else {
                return R.build(ResultStatusEnum.ERROR);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }

    }

}
